package com.ibeeking.gmc.global.g.rest.service.impl;


import com.ibeeking.gmc.global.g.rest.service.ITenantLegalInfoService;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.service.common.dos.TenantLegalInfoDO;
import com.ibeeking.gmc.global.service.common.param.TenantLegalInfoParam;
import com.ibeeking.gmc.global.service.common.vo.TenantLegalInfoVO;
import com.ibeeking.gmc.global.service.mapper.TenantLegalInfoMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName TenantLegalInfoServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-24 16:34
 **/
@Service
public class TenantLegalInfoServiceImpl extends ServiceImpl<TenantLegalInfoMapper, TenantLegalInfoDO> implements ITenantLegalInfoService {

    @Resource
    private TenantLegalInfoMapper tenantLegalInfoMapper;

    @Resource
    private FileOssProp fileOssProp;

    @Override
    public TenantLegalInfoVO view(Long id) {
        TenantLegalInfoDO tenantLegalInfoDO = tenantLegalInfoMapper.selectById(id);
        if (tenantLegalInfoDO == null) {
            return null;
        }
        return BeanUtil.convertBean(tenantLegalInfoDO, TenantLegalInfoVO.class);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        return tenantLegalInfoMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delByTenantId(Integer tenantId) {
        TenantLegalInfoVO vo = this.viewByTenantId(tenantId);
        if (vo == null) {
            throw new BusinessException("请求资源失败");
        }
        return this.del(vo.getId());
    }

    @Override
    public TenantLegalInfoVO viewByTenantId(Integer tenantId) {
        QueryWrapper<TenantLegalInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TenantLegalInfoDO::getTenantId, tenantId);
        TenantLegalInfoDO tenantLegalInfoDO = this.tenantLegalInfoMapper.selectOne(queryWrapper);
        if (tenantLegalInfoDO == null) {
            return null;
        }
        TenantLegalInfoVO vo = JsonUtils.fromJson(tenantLegalInfoDO, TenantLegalInfoVO.class);
        vo.setIdCardUp(UrlUtils.addUrlDomainName(this.fileOssProp.getHost(), vo.getIdCardUp()));
        vo.setIdCardDown(UrlUtils.addUrlDomainName(this.fileOssProp.getHost(), vo.getIdCardDown()));
        vo.setHandHoldIdCard(UrlUtils.addUrlDomainName(this.fileOssProp.getHost(), vo.getHandHoldIdCard()));
        return vo;
    }

    @Override
    public Boolean save(TenantLegalInfoParam tenantLegalInfoParam) {
        if (tenantLegalInfoParam.getIdCardTermType() == 2) {
            tenantLegalInfoParam.setIdCardTermEnd(null);
        } else {
            DataUtils.trueThrowException(StringUtils.isBlank(tenantLegalInfoParam.getIdCardTermEnd()), "请填写证件有效期截止时间");
        }
        TenantLegalInfoDO tenantLegalInfoDO = JsonUtils.fromJson(tenantLegalInfoParam, TenantLegalInfoDO.class);
        TenantLegalInfoVO legalInfoOld = viewByTenantId(tenantLegalInfoParam.getTenantId());
        Long userId = UserUtils.getUserId();
        if (legalInfoOld != null) {
            tenantLegalInfoDO.setId(legalInfoOld.getId());
            tenantLegalInfoDO.setModifyBy(userId);
            tenantLegalInfoDO.setModifyTime(LocalDateTime.now());
        } else {
            tenantLegalInfoDO.setCreateTime(LocalDateTime.now());
            tenantLegalInfoDO.setModifyTime(LocalDateTime.now());
            tenantLegalInfoDO.setCreateBy(userId);
        }
        tenantLegalInfoDO.setIdCardUp(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), tenantLegalInfoDO.getIdCardUp()));
        tenantLegalInfoDO.setIdCardDown(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), tenantLegalInfoDO.getIdCardDown()));
        tenantLegalInfoDO.setHandHoldIdCard(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), tenantLegalInfoDO.getHandHoldIdCard()));

        return saveOrUpdate(tenantLegalInfoDO);
    }


}
